using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

using IGlobalist.Net.Rtp;

namespace ServerConsole
{
    class Program
    {
        private RtpSession serverSession = null;
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Start(args);
            Console.WriteLine("Server started ...");
            Console.ReadLine();
            p.serverSession.Dispose();
            p.serverSession = null;
        }
        private void Start(string[] args)
        {
            int port = 12000;
            IPAddress addr = GetIP();
            if (args.Length != 0)
            {
                addr = IPAddress.Parse(args[0]);
                if (args.Length > 1)
                    port = Convert.ToInt32(args[1]);
            }
            IPEndPoint ep = new IPEndPoint(addr, port);
            IPEndPoint fakeEp = new IPEndPoint(IPAddress.Loopback, port);
            RtpParticipant part = new RtpParticipant("connect-point", "connect-point", false, true, true, ep);
            serverSession = new RtpSession(ep, fakeEp, part, true, true, true);
        }
        private static IPAddress GetIP()
        {
            foreach (IPAddress a in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (!IPAddress.IsLoopback(a) && a.AddressFamily == AddressFamily.InterNetwork)
                {
                    return a;
                }
            }
            return null;
        }
    }
}
